package userInterface;

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.TextArea;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.UndoManager;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * The Class GraphicComponents.
 * 
 */
public class GraphicComponents extends JFrame
{ 
	protected JMenuBar bar;
	protected JMenu fileMenu,editMenu,additiveEditMenu,macroMenu,searchMenu,SpellCheckerMenu;
	protected JMenuItem Newfile,Openfile,Save,Sava_As,Compare_as,Exit;
	protected JMenuItem Copy,Cut,Paste,Delete,Undo,Redo;
	protected JMenuItem StartRecording,StopRecording,PlayRecording,ClearRecording;
	protected JMenuItem Clear;
	protected JMenuItem Find,Replace;
	protected JMenuItem SpellChecker;
	protected JMenuItem Copyaddit,Cutaddit,Clearaddit;
	protected JMenuItem keyboard;
	protected JButton CheckWord,AddToTextArea,AddToDictionry,ExitSpell;
	public static JTextArea text,macrotextarea,lines;
    protected String shortcut="";
    protected int shortcutcode=0,count=0,globalindex=0,global=0;
    protected static int first =0;
    protected boolean isOn=false;
	protected String Storedmacro="";
	protected Object Record;
	protected UndoManager manager = new UndoManager();
	protected FileDialog dialogOpen,dialogSaveAs,dailogCompareTo;
	protected JTextField text1,text2,Counttext,text4,text5,text6,text7,SpellText1,SpellText2,SpellText3;
	protected TextArea SpelltextArea;
	protected JButton FindAll,FindNext,SearchCancel1,SearchCancel2,WordCount1,WordCount2,ReplaceNext,ReplaceAll;
	protected JFrame frame,Spellframe;
	protected JDialog CountDialog;
	protected JTabbedPane tab = new JTabbedPane();
	public static Checkbox FindExactWord ;
	public static Checkbox FindcaseSensitive;
	public static Checkbox ReplaceExactWord;
	public static Checkbox ReplacecaseSensitive;
	public static Checkbox MatchPrefix;
	public static Checkbox Unfixed;
	protected JPanel largeFindPanel = new JPanel(new BorderLayout());
	protected JPanel largeReplacePanel = new JPanel(new BorderLayout());
	protected JPanel SpellCheckerPanel=new JPanel(new BorderLayout());
	JScrollPane Scroll,SpellScroll;
	
	JPanel SpellTextPanel1=new JPanel(new BorderLayout());
	JPanel SpellTextPanel2=new JPanel(new BorderLayout());
	JPanel SpellButtonPanel = new JPanel();
	JPanel SouthSpell=new JPanel();
	JPanel NorthSpell=new JPanel();
	JPanel CenterSpell=new JPanel();
	JPanel TextPanel1 = new JPanel(new BorderLayout());
	JPanel TextPanel2 = new JPanel(new BorderLayout());
	JPanel FindPanel = new JPanel(new BorderLayout());
	JPanel checkBoxPanel1 = new JPanel();
	JPanel checkBoxPanel2 = new JPanel();
	JPanel radioPanel1 = new JPanel();
	JPanel radioPanel2 = new JPanel();
	JPanel SpellCheckBox=new JPanel();
	JPanel ReplacePanel = new JPanel(new BorderLayout());
	JPanel SearchPanel = new JPanel(new BorderLayout());
	JPanel mainpane = new JPanel(new BorderLayout());
	JPanel mainpane4 = new JPanel(new BorderLayout());
	StringSelection clipboardstring;
	Font font;
	JPanel mainpanel = new JPanel(new BorderLayout());
	String command="",SelectedText="",Stream="";
	int filelength,index;
	protected KeyEvent e;
	protected CaretEvent ea;
	protected static InputMap map;
	
	/**
	 * Instantiates a new graphic components.
	 * Creates all the user interfaces for the text editor
	 * @throws NullPointerException the null pointer exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public GraphicComponents() throws NullPointerException, IOException
    {
    	super("Empty Note");
    	initializeGraphicComponents();
    	createMenuBars();
    	createMenuItems();
    	text=new JTextArea();//instantiates text area
    	macrotextarea= new JTextArea();
    	lines = new JTextArea("1");
    	lines.setBackground(Color.LIGHT_GRAY);
		lines.setEditable(false);
		Scroll = new JScrollPane(text);//enables scroll
		Scroll.setRowHeaderView(lines);
		Scroll.setPreferredSize(new Dimension(500,400));
		mainpanel.add(Scroll,BorderLayout.CENTER);
		setContentPane(mainpanel);
		font=new Font("Arial",Font.PLAIN,13);
		text.setFont(font);
		text.requestFocus();
		map = text.getInputMap();
		text.getDocument().addUndoableEditListener(new UndoableEditListener() {
          public void undoableEditHappened(UndoableEditEvent e) {
              manager.addEdit(e.getEdit());
            }
		});
    }
	
	/**
	 * Initializes graphic components.
	 * Creates the JFrame, main panel, creates dialog boxes and GUI objects
	 */
	public void initializeGraphicComponents()
	{
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setPreferredSize(new Dimension(500, 400));
		pack();
		setLocationRelativeTo(mainpanel);
		bar=new JMenuBar();
		setJMenuBar(bar);
		dialogOpen=new FileDialog(this,"Open");
		dialogSaveAs=new FileDialog(this,"Save As",FileDialog.SAVE);
		dailogCompareTo=new FileDialog(this,"Compare to");
	}
	
	/**
	 * Creates the menu bars.
	 * This creates menus in the menu bar which was initialised in the initializeGraphicComponents() function
	 */
	public void createMenuBars()
	{
		fileMenu=new JMenu("File");
		editMenu=new JMenu("Edit");
		additiveEditMenu=new JMenu("Advanced Editing");
		macroMenu=new JMenu("Macro");
		searchMenu=new JMenu("Search");
		SpellCheckerMenu=new JMenu("Spell");
		bar.add(fileMenu);
		bar.add(editMenu);
		bar.add(additiveEditMenu);
		bar.add(macroMenu);
		bar.add(searchMenu);
		bar.add(SpellCheckerMenu);
	}
	
	/**
	 * This function creates menu items such as open, save as in the corresponding menus.
	 * 
	 */
	public void createMenuItems()
	{
		fileMenu.add(Newfile=new JMenuItem("New file"));
		fileMenu.add(Openfile=new JMenuItem("Open file"));
		fileMenu.add(Sava_As=new JMenuItem("Save as"));
		fileMenu.add(Save=new JMenuItem("Save"));
		fileMenu.add(Compare_as=new JMenuItem("Compare_as"));
		fileMenu.addSeparator();
		fileMenu.add(Exit=new JMenuItem("Exit"));
		editMenu.add(Copy=new JMenuItem("Copy"));
		editMenu.add(Cut=new JMenuItem ("Cut"));
		editMenu.add(Paste=new JMenuItem("Paste"));
		editMenu.add(Delete=new JMenuItem("Delete"));
		editMenu.add(Clear=new JMenuItem("Clear"));
		editMenu.addSeparator();
		editMenu.add(Undo=new JMenuItem("Undo"));
		editMenu.add(Redo=new JMenuItem("Redo"));
		additiveEditMenu.add(Copyaddit=new JMenuItem("Additive Copy"));
		additiveEditMenu.add(Cutaddit=new JMenuItem("Additive Cut"));
		additiveEditMenu.add(Clearaddit=new JMenuItem("Clear Additive"));
		macroMenu.add(StartRecording=new JMenuItem("Start Recording"));
		macroMenu.add(StopRecording=new JMenuItem("Stop Recording"));
		macroMenu.add(PlayRecording=new JMenuItem("Play Recording"));
		macroMenu.add(ClearRecording=new JMenuItem("Clear Recording"));
		searchMenu.add(Find=new JMenuItem("Find"));
		searchMenu.add(Replace=new JMenuItem("Replace"));
		SpellCheckerMenu.add(SpellChecker=new JMenuItem("Spell Checker"));
	}
	
	
public void createCountWindow(){
		
		CountDialog= new JDialog();
		CountDialog.setTitle("Count..."); 
		CountDialog.setPreferredSize(new Dimension(180, 160));
		CountDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE );
		CountDialog.pack();
		CountDialog.setVisible(true);
		CountDialog.setResizable(false);
		CountDialog.setLocation(500, 250);
		CountDialog.add(Counttext);
		CountDialog.requestFocus();	
	}
		
public void createcheckBox() {
		
		checkBoxPanel1 = new JPanel();
		checkBoxPanel2 = new JPanel();
		
		checkBoxPanel1.setLayout(new BoxLayout(checkBoxPanel1, BoxLayout.Y_AXIS));
		checkBoxPanel2.setLayout(new BoxLayout(checkBoxPanel2, BoxLayout.Y_AXIS));

		FindExactWord = new Checkbox ("Match Exact word", false);
	    FindcaseSensitive = new Checkbox ("Match Case", false);
	    ReplaceExactWord = new Checkbox ("Match Exact word", false);
	    ReplacecaseSensitive = new Checkbox ("Match Case", false);
	    	    
	    checkBoxPanel1.add(FindExactWord);
	    checkBoxPanel1.add(FindcaseSensitive);
	    
	    checkBoxPanel2.add(ReplaceExactWord);
	    checkBoxPanel2.add(ReplacecaseSensitive);
	    
	    FindPanel.add(checkBoxPanel1);	
	    ReplacePanel.add(checkBoxPanel2);
	}


public void createJButton(){
	
	FindAll=new JButton("Find All");
	FindNext=new JButton("Find Next");
	SearchCancel1=new JButton("Cancel...");
	SearchCancel2=new JButton("Cancel...");
	WordCount1=new JButton("Word Count ");
	WordCount2=new JButton("Word Count");
	ReplaceNext=new JButton("Replace Next");
	ReplaceAll=new JButton("Replace All");

	FindAll.setSize(10, 20);
	FindNext.setSize(10, 20);
	SearchCancel1.setSize(10, 20);
	WordCount1.setSize(10, 20);
	SearchCancel2.setSize(10, 20);
	WordCount2.setSize(10, 20);
	ReplaceNext.setSize(10, 20);
	ReplaceAll.setSize(10, 20);
	
	FindPanel.setLayout (new FlowLayout(FlowLayout.CENTER));
	ReplacePanel.setLayout (new FlowLayout(FlowLayout.CENTER));
	SpellButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
	
	FindPanel.add(FindAll);
	FindPanel.add(new JLabel(" "));
	FindPanel.add(FindNext);
	FindPanel.add(new JLabel(" "));
	FindPanel.add(WordCount1);
	FindPanel.add(new JLabel(" "));
	FindPanel.add(SearchCancel1);
	
	ReplacePanel.add(ReplaceNext);
	ReplacePanel.add(new JLabel(" "));
	ReplacePanel.add(ReplaceAll);
	ReplacePanel.add(new JLabel(" "));
	ReplacePanel.add(WordCount2);
	ReplacePanel.add(new JLabel(" "));
	ReplacePanel.add(SearchCancel2);
	
	SouthSpell.add(SpellButtonPanel,new FlowLayout(FlowLayout.RIGHT));

}
public void createJTextField(){
	
	SpelltextArea= new TextArea();
	text1=new JTextField(15);
	text2=new JTextField(11);
	text4=new JTextField(15);
	text5=new JTextField(11);
	Counttext=new JTextField();
	text6=new JTextField(11);
	text7=new JTextField(15);
	
	SpellText1=new JTextField(15);
	SpellText2=new JTextField();
	SpellText3=new JTextField();
	
	text2.setEditable(false);
	Counttext.setEditable(false);
	text5.setEditable(false);
	text6.setEditable(false);
	
	TextPanel1.setLayout(new FlowLayout(FlowLayout.CENTER));
	TextPanel2.setLayout(new FlowLayout(FlowLayout.CENTER));
	
	TextPanel1.add(text2);
	TextPanel1.add(text1);
	TextPanel2.add(text5);
	TextPanel2.add(text4);
	TextPanel2.setBorder(BorderFactory.createEmptyBorder(1, 20, 20, 20));
	TextPanel2.add(text6);
	TextPanel2.add(text7);
		text2.setText("Please Enter Words");
		text5.setText("Please Enter Words");
		text6.setText("Replace With");
		SpellText2.setText("Please Enter a Word");
		SpellText3.setText("Options");
		font=new Font("Arial",Font.BOLD,12);
		Counttext.setFont(font);
		text2.setFont(font);
		text5.setFont(font);
		text6.setFont(font);
}
public void createSpellWindow(){
	RePreviousPanels();
	Spellframe=new JFrame("Spell Check Window");
	Spellframe.setPreferredSize(new Dimension(750,350));
	Spellframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	Spellframe.pack();
	Spellframe.setVisible(true);
	Spellframe.setResizable(false);
	createJTextField1();
	createcheckBox1();
	createJButton1();
	SpellCheckerPanel.add(SouthSpell,BorderLayout.SOUTH);
	SpellCheckerPanel.add(CenterSpell,BorderLayout.CENTER);
	SpellCheckerPanel.add(NorthSpell,BorderLayout.NORTH);   
	Spellframe.add(SpellCheckerPanel);
	
}
private void createJButton1() {
	CheckWord=new JButton("Click to Check Word");
	AddToTextArea=new JButton("Add To the TextArea");
	AddToDictionry=new JButton("Add To the Dictionry");
	ExitSpell=new JButton("Exit Spell Check");
	
	SpellButtonPanel.add(CheckWord);
	SpellButtonPanel.add(AddToTextArea);
	SpellButtonPanel.add(AddToDictionry);
	SpellButtonPanel.add(ExitSpell);
	
	CheckWord.setSize(10,20);
	AddToTextArea.setSize(10, 20);
	AddToDictionry.setSize(10,20);
	ExitSpell.setSize(10,20);
	
	SpellButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));		
	SouthSpell.add(SpellButtonPanel,new FlowLayout(FlowLayout.RIGHT));
	
}

private void createcheckBox1() {
	
	SpellCheckBox.setLayout(new BoxLayout(SpellCheckBox, BoxLayout.Y_AXIS));
    MatchPrefix = new Checkbox ("Match Prefix", false);
    Unfixed = new Checkbox ("Unfiex", false);
    SpellCheckBox.add(MatchPrefix);
    SpellCheckBox.add(Unfixed);    
    SouthSpell.add(SpellCheckBox,new FlowLayout(FlowLayout.LEFT));
	
}

private void createJTextField1() {
	SpelltextArea= new TextArea();
	
	SpellText1=new JTextField(15);
	SpellText2=new JTextField();
	SpellText3=new JTextField();
	
	SpellScroll =new JScrollPane(SpelltextArea);
	SpellScroll = new JScrollPane(SpelltextArea);

	SpellText2.setEditable(false);
	SpellText3.setEditable(false);
	
	NorthSpell.setLayout(new FlowLayout(FlowLayout.LEADING));
	
	NorthSpell.add(SpellText2);
	NorthSpell.add(SpellText1);
	
	CenterSpell.add(SpellText3);
	CenterSpell.add(SpelltextArea);

	SpellText2.setText("Please Enter a Word");
	SpellText3.setText("Options");
	
}

public void createSearchWindow(){
	ReomovePreviousPanels();
	tab = new JTabbedPane();
	tab.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
	frame=new JFrame("Search Window");
	frame.add(tab);
	frame.setPreferredSize(new Dimension(450,220));
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );
	frame.pack();
	frame.setVisible(true);
	frame.setResizable(false);

	largeFindPanel.add(TextPanel1,BorderLayout.NORTH);
	largeFindPanel.add(FindPanel,BorderLayout.CENTER);
	
	largeReplacePanel.add(TextPanel2,BorderLayout.NORTH);
	largeReplacePanel.add(ReplacePanel,BorderLayout.CENTER);

	createJTextField();
	createJButton();
	createcheckBox(); 
	
	tab.add("Find",largeFindPanel);
	tab.add("Replace",largeReplacePanel);
	}

public void ReomovePreviousPanels() {
		
		TextPanel2.removeAll(); 
		TextPanel1.removeAll();
		ReplacePanel.removeAll(); 
		FindPanel.removeAll();
		largeFindPanel.removeAll();
		largeReplacePanel.removeAll();
	}
public void RePreviousPanels(){
	
	SouthSpell.removeAll();
	CenterSpell.removeAll();
	NorthSpell.removeAll();
	SpellCheckerPanel.removeAll();
	SpellButtonPanel.removeAll();
	SpellCheckBox.removeAll();
}
}